import {post, put} from "@/api/request";
import {
    deleteQuestion,
    editQuestion,
    fetchQuestionType,
    fetchSubject,
    fetchSubjectChild,
    insertQuestion
} from "@/api/exam";
import {mapState} from "vuex";

export const questionComponents = {
    data() {
        let checkNumber = (rule, value, callback) => {
            if (!value) {
                return callback(new Error("题型分数不能为空"));
            }
            if (!Number.isInteger(value)) {
                callback(new Error("请输入数字值"));
            } else if (value < 0) {
                callback(new Error("请输入大于0的数字"));
            } else if (value > 100) {
                callback(new Error("请输入小于等于100的数字"));
            } else {
                callback();
            }
        };
        let checkPaperNumber = (rule, value, callback) => {
            if (!value) {
                return callback(new Error("不能为空"));
            }
            if (!Number.isInteger(value)) {
                callback(new Error("请输入数字值"));
            }
            if (value < 0) {
                callback(new Error("请输入大于0的数字值"));
            } else if (value > 750) {
                callback(new Error("请输入小于750的数字值"));
            } else {
                callback();
            }
        };
        let checkColumn = (rule, value, callback) => {
            if (!value) {
                return callback(new Error("栏目不能为空"));
            }
            callback();
        };
        return {
            isEdit: false,
            paperTypeOptions: [
                {
                    label: "全真",
                    value: 0
                },
                {
                    label: "模拟",
                    value: 1
                }
            ],
            formLabelWidth: "80px",
            questionForm: {
                mainStem: "",
                answer: "",
                desc: "",
                score: 1,
                heOptions: [
                    {
                        name: "A",
                        value: "",
                        isAnswer: false,
                        isQuill: false
                    }, {
                        name: "B",
                        value: "",
                        isAnswer: false,
                        isQuill: false
                    },
                ],
                fillOptions: [
                    {
                        name: "",
                        value: "",
                        isQuill: false
                    },
                ],
                diffVl: "easy",
                isAnswerQuill: false,
            },
            paperForm: {
                id: null,
                categoryId: null,
                remark: "",
                name: "",
                sourceYear: null,
                examTime: 0,
                totalScore: 0,
                paperType: 0,
                integrityState: 0,
                passScore: 0,
                isHide: 0,
                sourceType: 0,
                columnId: null,
                content: []
            },
            chapterForm: {},
            questionTypeForm: {
                name: "",
                desc: "",
                score: 0
            },
            questionTypeRules: {
                desc: [
                    {required: true, message: "请输入大题型描述", trigger: "blur"},
                    {min: 1, max: 200, message: "长度在 1 到 200 个字符", trigger: "blur"}
                ],
                name: [
                    {required: true, message: "请输入大题型名称", trigger: "blur"},
                    {min: 1, max: 30, message: "长度在 1 到 30 个字符", trigger: "blur"}
                ],
                score: [
                    {required: true, validator: checkNumber, trigger: "blur"}
                ]
            },
            addQuestionText: "添加单选题",
            addQuestionType: 0,
            quTypeItems: [],
            paperRules: {
                categoryId: [
                    {required: true, message: "请选择所属学科", trigger: "change"}
                ],
                columnId: [
                    {required: true, validator: checkColumn, trigger: "blur"}
                ],
                paperType: [
                    {required: true, message: "请选择试卷类型", trigger: "change"}
                ],
                name: [
                    {required: true, message: "请输入试卷名称", trigger: "blur"},
                    {min: 3, max: 30, message: "长度在 3 到 30 个字符", trigger: "blur"}
                ],
                sourceYear: [
                    {required: true, message: "请选择试卷年份", trigger: "change"}
                ],
                examTime: [
                    {required: true, validator: checkPaperNumber, trigger: "blur"}
                ],
                totalScore: [
                    {required: true, validator: checkPaperNumber, trigger: "blur"}
                ],
                passScore: [
                    {required: true, validator: checkPaperNumber, trigger: "blur"}
                ],
            },
            chapterRules: {
                categoryId: [
                    {required: true, message: "请选择一个学科", trigger: "change"}
                ],
            },
            quBoxSort: [],
            qTypeNameArray: []
        }
    },
    methods: {
        // 收起试卷
        putAway(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (this.isPutPaper) {
                        this.isPutPaper = false;
                        this.saveBtnTxt = "收起试卷信息";
                    } else {
                        this.isPutPaper = true;
                        this.saveBtnTxt = "展开试卷信息";
                    }
                    if (this.isPutPaper && this.paperForm.name === "") {

                        // 保存到后端
                        post("exam/paper/update", this.paperForm).then(data => {
                            this.paperForm.id = data.id
                        })
                    }
                } else {
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        // 单选题
        singleChoice(index, index1, id, question) {
            this.$emit("open-question-dialog", 1, index, index1, id, question)
        },
        // 多选题
        multipleChoice(index, index1, id, question) {
            this.$emit("open-question-dialog", 2, index, index1, id, question)
        },
        // 判断题
        judgmentChoice(index, index1, id, question) {
            this.$emit("open-question-dialog", 3, index, index1, id, question)
        },
        // 填空题
        completionChoice(index, index1, id, question) {
            this.$emit("open-question-dialog", 4, index, index1, id, question)
        },
        // 问答题
        questionChoice(index, index1, id, question) {
            this.$emit("open-question-dialog", 5, index, index1, id, question)
        },
        // 复合题
        combinationChoice(index, index1, id) {
            this.$emit("open-question-dialog", 6, index, index1, id)
        },
        //-----------------------------------------------------------------
        // 保存大题型
        saveQuestionType(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    let itemData = {
                        name: this.questionTypeForm.name,
                        score: 0,
                        remark: this.questionTypeForm.desc,
                        isShow: false,
                        quBoxItems: []
                    }
                    let paperContent = {
                        tyName: this.questionTypeForm.name,
                        desc: this.questionTypeForm.desc,
                        score: 0,
                        sort: 0,
                        qs: []
                    }
                    if (this.isEditBigQuType) {
                        // 编辑
                        this.quTypeItems[this.qtypeIndex].name = this.questionTypeForm.name;
                        this.quTypeItems[this.qtypeIndex].remark = this.questionTypeForm.desc;
                        if (this.addStyle === "0") {
                            paperContent.qs = this.paperForm.content[this.qtypeIndex].qs
                            this.paperForm.content[this.qtypeIndex] = paperContent;
                        } else {
                            paperContent.qs = this.chapterForm.content[this.qtypeIndex].qs
                            this.chapterForm.content[this.qtypeIndex] = paperContent;
                        }
                    } else {
                        this.quTypeItems.push(itemData)
                        if (this.addStyle === "0") {
                            this.paperForm.content.push(paperContent);
                        } else {
                            this.chapterForm.content.push(paperContent);
                        }
                    }
                    this.questionTypeDialog = false;
                } else {
                    return false;
                }
            });
        },
        // 新增试题
        submitQuestion(qForm) {
            this.questionForm = qForm;
            let qtypeMap = {"1": "单选题", "2": "多选题", "3": "判断题", "4": "填空题", "5": "问答题", "6": "复合题","7":"不定项选择题"};
            let dataForm = {
                qType: qtypeMap[this.addQuestionType],
                score: this.questionForm.score,
                stem: this.questionForm.mainStem,
                answerDesc: this.questionForm.desc,
                isShow: false,
                typeId: this.questionForm.typeId,
                diff: this.questionForm.diffVl,
                sourceType: this.questionForm.sourceType
            };
            if (this.isEdit) {
                if (this.isCombination) {
                    this.fzQuDataModel("comb", dataForm, "edit");
                } else {
                    this.fzQuDataModel("normal", dataForm, "edit");
                }
            } else {
                if (this.isCombination) {
                    dataForm.sourceType = "combination"
                    this.fzQuDataModel("comb", dataForm, "add");
                } else {
                    dataForm.sourceType = 1//code
                    // dataForm.sourceType = "importHand"
                    this.fzQuDataModel("normal", dataForm, "add");
                }
            }

        },
        // 添加试题
        fzQuDataModel(type, dataForm, operateType) {
            let qtMap = {
                "1": "singleChoice",
                "2": "multipleChoice",
                "3": "judgment",
                "4": "fill",
                "5": "text",
                "6": "combination",
                "7":"indefiniteMultipleChoice"
            };
            let answer = "";
            // 封装试题保存model
            let quData = {
                firstType: this.addQuestionType,
                knowledgePoint: "",
                difficulty: dataForm.diff,
                sourceType: dataForm.sourceType,
                knowledgeLevel: 5,
                isHide: false,
                score: dataForm.score
            }
            let len;
            if (type === "normal") {
                // 正常题
                len = this.quTypeItems[this.qtypeIndex].quBoxItems.length + 1;
                dataForm.children = [];
            } else {
                // 复合题子题
                len = this.quTypeItems[this.qtypeIndex].quBoxItems[this.qtypeChildIndex].children.length + 1;
                quData.sourceId = this.questionForm.sourceId;
            }
            dataForm.xh = len + "、";
            // 单选题 || 多选题
            if (this.addQuestionType === 1 || this.addQuestionType === 2 || this.addQuestionType === 7) {
                let multAnswer = [];
                dataForm.options = this.questionForm.heOptions.map(v => {
                    if (v.isAnswer) {
                        if (answer !== "") {
                            answer += ","
                        }
                        answer += v.name
                        multAnswer.push(v.name);
                    }
                    let htmlObj = {
                        tp: "html",
                        vl: v.value
                    }
                    return {
                        option: v.name,
                        content: [htmlObj]
                    };
                });
                if (this.addQuestionType === 2 || this.addQuestionType === 7) {
                    dataForm.answer = multAnswer;
                } else {
                    dataForm.answer = answer;
                }
            }
            // 判断题
            if (this.addQuestionType === 3) {
                let htmlObj = {
                    "tp": "text",
                    "vl": "正确"
                }
                let wrongObj = {
                    "tp": "text",
                    "vl": "错误"
                }
                dataForm.options = [{option: 1, content: [htmlObj]}, {option: 0, content: [wrongObj]}];
                dataForm.answer = this.questionForm.answer;
            }
            // 填空题
            if (this.addQuestionType === 4) {
                let fillAnswer = [];
                dataForm.options = this.questionForm.fillOptions.map(v => {
                    let fData = {pt: [], vl: [{tp: "text", vl: v.value,}]}
                    fillAnswer.push(fData)
                    return {tp: ["text"]}
                });
                dataForm.answer = fillAnswer;
            }
            // 问答题
            if (this.addQuestionType === 5) {
                dataForm.options = [{tp: ["html"]}];
                let answerObjet = {
                    tp: "html",
                    vl: this.questionForm.answer
                }
                dataForm.answer = [{pt: [], vl: [answerObjet]}];
            }
            // 复合题
            if (this.addQuestionType === 6) {
                dataForm.children = [];
            }
            let htmlObjet = {
                tp: "html",
                vl: dataForm.stem
            }
            let descObjet = {
                tp: "html",
                vl: dataForm.answerDesc
            }
            if (this.addStyle === "0") {
                //试卷
                quData.categoryId = this.paperForm.categoryId
            } else {
                // 章节
                quData.categoryId = this.chapterForm.categoryId
            }
            if (this.addQuestionType !== 6) {
                quData.quOption = dataForm.options;
                quData.answer = dataForm.answer;
                quData.answerDesc = [descObjet];
            }
            quData.stem = [htmlObjet];
            quData.typeId = dataForm.typeId;
            if (operateType === "edit") {
                quData.id = this.questionForm.id;
                if (type === "comb") {
                    this.quTypeItems[this.qtypeIndex].quBoxItems[this.qtypeChildIndex].children[this.qtypeChildChildIndex].answerDesc = quData.answerDesc;
                    this.quTypeItems[this.qtypeIndex].quBoxItems[this.qtypeChildIndex].children[this.qtypeChildChildIndex].stem = quData.stem;
                    this.quTypeItems[this.qtypeIndex].quBoxItems[this.qtypeChildIndex].children[this.qtypeChildChildIndex].score = this.questionForm.score;
                    this.quTypeItems[this.qtypeIndex].quBoxItems[this.qtypeChildIndex].children[this.qtypeChildChildIndex].diff = this.questionForm.diffVl;
                    this.quTypeItems[this.qtypeIndex].quBoxItems[this.qtypeChildIndex].children[this.qtypeChildChildIndex].options = dataForm.options;
                    this.quTypeItems[this.qtypeIndex].quBoxItems[this.qtypeChildIndex].children[this.qtypeChildChildIndex].answer = dataForm.answer;
                    this.quTypeItems[this.qtypeIndex].quBoxItems[this.qtypeChildIndex].children[this.qtypeChildChildIndex].typeId = dataForm.typeId;
                    this.addQuestionDialog = false;
                } else {
                    this.quTypeItems[this.qtypeIndex].quBoxItems[this.qtypeChildIndex].answerDesc = quData.answerDesc;
                    this.quTypeItems[this.qtypeIndex].quBoxItems[this.qtypeChildIndex].stem = quData.stem;
                    this.quTypeItems[this.qtypeIndex].quBoxItems[this.qtypeChildIndex].score = this.questionForm.score;
                    this.quTypeItems[this.qtypeIndex].quBoxItems[this.qtypeChildIndex].diff = this.questionForm.diffVl;
                    this.quTypeItems[this.qtypeIndex].quBoxItems[this.qtypeChildIndex].options = dataForm.options;
                    this.quTypeItems[this.qtypeIndex].quBoxItems[this.qtypeChildIndex].answer = dataForm.answer;
                    this.quTypeItems[this.qtypeIndex].quBoxItems[this.qtypeChildIndex].typeId = dataForm.typeId;
                    this.addQuestionDialog = false;
                }
            }
            dataForm.stem = quData.stem;
            dataForm.answerDesc = quData.answerDesc;
            this.addQuestion(quData, dataForm);
        },

        // 修改试题弹出框
        editQuestionDialog(data) {

            this.isEdit = true;
            let question = data[0];
            this.addQuestionText = "修改" + question.qType;
            let qAnswer = question.answer;
            let type = 1;
            console.log("111",question.qType)
            if (question.qType === "单选题" || question.qType === "多选题" || question.qType === "不定项选择题") {
                if (question.qType === "多选题") {
                    type = 2;
                }
                console.log("question.qType",question.qType)
                if (question.qType === "不定项选择题") {
                    type = 7;
                }

                let dataOptions = question.options;
                let quData = [];
                dataOptions.map(v => {
                    let dd = {};
                    dd.name = v.option;
                    let opContent = "";
                    if (v.content.length > 0) {
                        opContent = this.getPublicMap(v.content);
                    }
                    dd.value = opContent;
                    dd.isQuill = /<[^>]*>/.test(dd.value);
                    dd.isAnswer = false;
                    if (type === 2 || type === 7) {
                        let dAnswer = "";
                        if(typeof question.answer ==="string" ){
                           question.answer =[...question.answer]
                        }
                        question.answer.map(q => {
                            if (v.option === q) {
                                dd.isAnswer = true;
                                dAnswer += q;
                            }
                        })
                        qAnswer = dAnswer;
                    }
                    if (type === 1) {
                        if (v.option === question.answer) {
                            dd.isAnswer = true;
                        }
                    }
                    quData.push(dd)
                })
                this.questionForm.heOptions = quData;
            }
            if (question.qType === "判断题") {
                type = 3;
            }
            if (question.qType === "填空题") {
                type = 4;
                let quData = [];
                let qa = question.answer;
                let fillAnswer = "";
                qa.map(v => {
                    let da = {
                        name: "",
                        value: "",
                        isQuill: false
                    };
                    let fillContent = "";
                    if (v.vl.length > 0) {
                        fillContent = this.getPublicMap(v.vl);
                        da.name = fillContent;
                        da.value = fillContent;
                        da.isQuill = /<[^>]*>/.test(da.value);
                    }
                    quData.push(da)
                    fillAnswer += da.value;
                });
                this.questionForm.fillOptions = quData;

                qAnswer = question.answer;
            }
            if (question.qType === "问答题") {
                type = 5;
                let quTextContent = "";
                if (question.answer !== null) {
                    question.answer.map(as => {
                        quTextContent += this.getPublicMap(as.vl)
                    });
                }
                qAnswer = quTextContent;
            }
            if (question.qType === "复合题") {
                type = 6;
                this.questionForm.isMainQuill = true;
            }
            // 处理题干
            let stemContent = "";
            if (question.stem !== null) {
                stemContent = this.getPublicMap(question.stem)
            }
            this.questionForm.mainStem = stemContent;
            // 处理解析
            let descContent = "";
            if (question.answerDesc !== null && question.answerDesc !==undefined) {
                descContent = this.getPublicMap(question.answerDesc)
            }
            this.questionForm.desc = descContent;
            this.questionForm.score = question.score;
            this.questionForm.diffVl = question.diff;
            this.questionForm.id = question.id;
            this.questionForm.answer = qAnswer;
            this.questionForm.typeId = question.typeId;
            this.questionForm.sourceType = question.sourceType;
            this.addQuestionType = type;
            this.qtypeIndex = data[1];
            this.qtypeChildIndex = data[2];
            this.qtypeChildChildIndex = data[3];
            this.isCombination = data[3] !== -1;
            this.findQType(this.addQuestionType);
        },

        getPublicMap(mapVl) {
            let content = "";
            mapVl.map(st => {
                if (st.tp === "img") {
                    let srcStr = "https://vipcdn.langrundata.com/" + st.vl;
                    content += "<img src=\"" + srcStr + "\" />"
                } else if (st.tp === "audio") {
                    let srcStr = "https://vipcdn.langrundata.com/" + st.vl;
                    content += "<audio src=\"" + srcStr + "\" controls='controls'/>"
                } else {
                    content += st.vl;
                }
            })
            return content
        },
        // 添加文字标题
        addTypeText(type) {
            switch (type) {
            case 1:
                this.addQuestionText = "添加单选题";
                break;
            case 2:
                this.addQuestionText = "添加多选题";
                break;
            case 3:
                this.addQuestionText = "添加判断题";
                break;
            case 4:
                this.addQuestionText = "添加填空题";
                break;
            case 5:
                this.addQuestionText = "添加问答题";
                break;
            default:
                this.addQuestionText = "添加复合题";
            }
        },
        // 根据题型查题型别名
        findQType(vl) {
            let qtMap = {
                "1": "singleChoice",
                "2": "multipleChoice",
                "3": "judgment",
                "4": "fill",
                "5": "text",
                "6": "combination",
                "7": "indefiniteMultipleChoice"
            };
            fetchQuestionType({firstType: qtMap[vl]}).then(res => {
                if (res) {
                    this.qTypeNameArray = res;
                    console.log(res,"999999999999999999")
                }
            })
        },
        // 写入试题
        addQuestion(quData, dataForm) {
            if (quData.categoryId === null) {
                return this.$message.warning("请选择试题所属科目")
            }
            if (quData.id !== null && quData.id !== undefined) {
                editQuestion(quData).then(res => {
                    if (res) {
                        this.$message({
                            message: "恭喜你，编辑试题成功",
                            type: "success"
                        });
                        let tScore = 0;
                        if (this.quTypeItems[this.qtypeIndex].quBoxItems.length > 0) {
                            this.quTypeItems[this.qtypeIndex].quBoxItems.map(v => {
                                tScore += v.score
                            });
                        }
                        this.quTypeItems[this.qtypeIndex].score = tScore;

                        if (this.addStyle === "0") {
                            this.paperForm.content[this.qtypeIndex].qs[this.qtypeChildIndex].score = quData.score;
                            this.paperForm.content[this.qtypeIndex].score = tScore;
                        } else {
                            this.chapterForm.content[this.qtypeIndex].qs[this.qtypeChildIndex].score = quData.score;
                            this.chapterForm.content[this.qtypeIndex].score = tScore;
                        }
                        this.initQuestionForm();
                    }
                })
            } else {
                insertQuestion(quData).then(res => {
                    if (res) {
                        let qData = {
                            id: res.id,
                            score: quData.score,
                        }
                        dataForm.id = res.id;
                        if (this.isCombination) {
                            dataForm.diff = res.difficulty;
                            this.quTypeItems[this.qtypeIndex].quBoxItems[this.qtypeChildIndex].children.push(dataForm);
                        } else {
                            this.quTypeItems[this.qtypeIndex].quBoxItems.push(dataForm);
                            let tScore = 0;
                            if (this.quTypeItems[this.qtypeIndex].quBoxItems.length > 0) {
                                this.quTypeItems[this.qtypeIndex].quBoxItems.map(v => {
                                    tScore += v.score
                                });
                            }
                            this.quTypeItems[this.qtypeIndex].score = tScore;
                            if (this.addStyle === "0") {
                                qData.sort = this.paperForm.content[this.qtypeIndex].qs.length + 1;
                                this.paperForm.content[this.qtypeIndex].qs.push(qData);
                                this.paperForm.content[this.qtypeIndex].score = tScore;
                            } else {
                                qData.sort = this.chapterForm.content[this.qtypeIndex].qs.length + 1;
                                this.chapterForm.content[this.qtypeIndex].qs.push(qData);
                                this.chapterForm.content[this.qtypeIndex].score = tScore;
                            }
                        }
                        // 清空试题弹框
                        this.initQuestionForm();
                        this.$message({
                            message: "恭喜你，写入试题成功",
                            type: "success"
                        });
                    } else {
                        this.$message.error("错了哦，写入试题失败");
                    }
                });
            }
        },
        initQuestionForm() {
            this.questionForm = {
                mainStem: "",
                answer: "",
                desc: "",
                score: 1,
                heOptions: [
                    {
                        name: "A",
                        value: "",
                        isAnswer: false,
                        isQuill: false
                    }, {
                        name: "B",
                        value: "",
                        isAnswer: false,
                        isQuill: false
                    },
                ],
                fillOptions: [
                    {
                        name: "",
                        value: "",
                        isQuill: false
                    },
                ],
                diffVl: "easy",
                isAnswerQuill: false,
                typeId: "",
            };
        },
        // 操作试题
        operateQuestion(data) {
            switch (data[0]) {
            case "del":
                if (data[4] !== null && data[4] !== undefined) {
                    // 删除复合题下的小题 复合题分数不变
                    this.quTypeItems[data[1]].quBoxItems[data[2]].children.splice(data[3], 1);
                    deleteQuestion({id: data[4].id}).then(res => {
                        if (res) {
                            this.$message({
                                message: "恭喜你，删除试题成功",
                                type: "success"
                            });
                        } else {
                            this.$message.error("错了哦，删除试题失败");
                        }
                    });
                } else {
                    if (this.quTypeItems[data[1]].score !== null && this.quTypeItems[data[1]].score > 0) {
                        this.quTypeItems[data[1]].score = this.quTypeItems[data[1]].score - data[3].score;
                    }
                    this.quTypeItems[data[1]].quBoxItems.splice(data[2], 1);
                    this.delPaperQuestion(data[1], data[3], this.quTypeItems[data[1]].score)
                }
                break;
            case "put":
                this.quTypeItems[data[1]].quBoxItems[data[2]].isShow = true;
                break;
            case "open":
                this.quTypeItems[data[1]].quBoxItems[data[2]].isShow = false;
                break;
            case "putChild":
                this.quTypeItems[data[1]].quBoxItems[data[2]].children[data[3]].isShow = true;
                break;
            case "openChild":
                this.quTypeItems[data[1]].quBoxItems[data[2]].children[data[3]].isShow = false;
                break;
            default:
                break;
            }
        },
        // 删除试卷试题
        delPaperQuestion(index, question, score) {
            if (this.addStyle === "0") {
                for (let i = 0; i < this.paperForm.content[index].qs.length; i++) {
                    if (this.paperForm.content[index].qs[i].id === question.id) {
                        this.paperForm.content[index].qs.splice(i, 1)
                    }
                }
                this.paperForm.content[index].score = score;
            } else {
                for (let i = 0; i < this.chapterForm.content[index].qs.length; i++) {
                    if (this.chapterForm.content[index].qs[i].id === question.id) {
                        this.chapterForm.content[index].qs.splice(i, 1)
                    }
                }
                this.chapterForm.content[index].score = score;
            }
        },
        // 添加大题型
        addBigQuestionType() {
            this.isQuTypeText = "添加大题型";
            this.questionTypeDialog = true;
            this.isEditBigQuType = false;
            this.questionTypeForm = {};
        },
        // 修改大题型
        editQuestionType(index, item) {
            this.isQuTypeText = "编辑大题型";
            this.questionTypeForm.name = item.name;
            this.questionTypeForm.desc = item.remark;
            this.questionTypeForm.type = item.type;
            this.questionTypeForm.score = item.score;
            this.questionTypeDialog = true;
            this.isEditBigQuType = true;
            this.qtypeIndex = index;
        },
        // 删除题型结构
        deleteQuestionType(index, item) {
            this.quTypeItems.splice(index, 1);
            // 删除试卷的题型
            this.paperForm.content.splice(index, 1);
        },
        // 收起题型结构
        putAwayQuestionType(index) {
            this.quTypeItems[index].isShow = !this.quTypeItems[index].isShow;
        },
        // 展开题型结构
        openQuestionType(index) {
            this.quTypeItems[index].isShow = !this.quTypeItems[index].isShow;
        },
        // 渲染数据
        xrQuestion(res, type) {
            let qtMap = {
                "singleChoice": "单选题",
                "multipleChoice": "多选题",
                "judgment": "判断题",
                "fill": "填空题",
                "text": "问答题",
                "combination": "复合题"
            };
            if (type === 0) {
                this.paperForm = res;
                // this.addStyle = "0";
            } else {
                // this.addStyle = "1";
                this.chapterForm.content = res.content
            }
            if (res.content !== null && res.content.length > 0) {
                res.content.map(v => {
                    let quData = {
                        name: v.tyName,
                        score: v.score,
                        remark: v.desc,
                        isShow: false,
                        quBoxItems: []
                    }
                    if (v.qs.length > 0) {
                        v.qs.forEach((m, index) => {
                            fetchSubject({id: m.id}).then(res => {
                                if (res !== null) {
                                    let question = {
                                        qType: this.status[res.firstType],
                                        stem: res.stem,
                                        isShow: false,
                                        children: [],
                                        diff: res.difficulty,
                                        id: m.id,
                                        typeId: res.typeId,
                                        score: res.score,
                                        sort: m.sort,
                                        sourceId:res.sourceId,
                                        sourceType: res.sourceType
                                    }
                                    if (res.firstType === "combination") {
                                        // 复合题
                                        fetchSubjectChild({id: m.id}).then(re => {
                                            re.map((q, index2) => {
                                                let question1 = {
                                                    qType: this.status[q.firstType],
                                                    stem: q.stem,
                                                    score: q.score,
                                                    options: q.quOption,
                                                    answer: q.answer,
                                                    answerDesc: q.answerDesc,
                                                    isShow: false,
                                                    diff: q.difficulty,
                                                    id: q.id,
                                                    typeId: q.typeId,
                                                    sourceId:q.sourceId,
                                                    sourceType: q.sourceType
                                                }
                                                question.children.push(question1);
                                            })
                                        })
                                    } else {
                                        question.options = res.quOption;
                                        question.answer = res.answer;
                                        question.answerDesc = res.answerDesc;
                                    }
                                    quData.quBoxItems.push(question)

                                }
                            })
                        })
                    }
                    this.quTypeItems.push(quData);

                });
            }
        },
        // 排序
        sortKey(a, b) {
            return a.sort - b.sort
        }
    },
    computed: {
        ...mapState({
            status: state => state.status.questionFirstType,
        }),
        quTypeItemsSort: function () {
            return this.quTypeItems.map(q => {
                q.quBoxItems.sort(this.sortKey)
                return q;
            });
        }
    },
}
